<?php

namespace addons\mental\controller;

use think\Db;


/**
 * 余额管理接口
 */
class Moneylog extends Base
{
    protected $noNeedLogin = ['getChanneltype'];
    
    
    
    /**
     * 提现(addons/mental/moneylog/withdrawal)
     * @param string $amount 提现金额
     * @param string $type 账号类型:1=微信,2=支付宝
     * @param string account_number 提现账号
     */
    public function withdrawal()
    {
        $uid = $this->auth->id;
        $amount = $this->request->param('amount');
        $type = $this->request->param('type');
        $account_number = $this->request->param('account_number');
        
        if (!$amount&&!$type&&!$account_number) {
            $this->error('数据错误');
        }
        
        if ($amount > $this->auth->money) {
            $this->error(__('余额不足'));
        }
        
        if ($amount < 1) {
            $this->error('金额不能低于1');
        }
        
        $data = [
            'user_id'=>$uid,
            'amount'=>$amount,
            'type'=>$type,
            'account_number'=>$account_number,
            'status'=>0,
            'createtime'=>time(),
            'updatetime'=>time(),
            ];

        // 启动事务
        Db::startTrans();
        try{
            Db::name('withdrawal')->insert($data);
            \app\common\model\User::money(-$amount,$uid,'提现',6);
            
            // 提交事务
            Db::commit();    
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
//            var_dump($e->getMessage());die;
            $this->error('失败:'.$e->getMessage());
        }
        $this->success('成功');
    }
    
    
    
    /**
     * 提现记录(addons/mental/moneylog/withdrawallist)
     */
    public function withdrawallist()
    {
        $uid = $this->auth->id;
        $list = Db::name('withdrawal')->where('user_id',$uid)->order('id DESC')->paginate(20);
        
        $this->success('成功',$list);
    }
    
    
    /**
     * 收益记录(addons/mental/moneylog/moneylist)
     */
    public function moneylist()
    {
        $where = [];
        $uid = $this->auth->id;
        $role=$this->request->param('role');
        if($role=='PSYCHOLOGIST'){
            $where['is_source']= 1;
        }
        if($role=='USER'){
            $where['is_source']= 2;
        }
        $list = Db::name('user_money_log')->where('user_id',$uid)->whereIn('type',[4,5,7])->field('id,money,memo,createtime')->order('id DESC')->paginate(20);
        
        $this->success('成功',$list);
    }
    
    
    /**
     * 分销订单管理(addons/mental/moneylog/fxorderlist)
     * @param string $main_id 量表id
     * @param string $channel_type 渠道
     * @param string $stime 开始时间
     * @param string $etime 结束时间
     */
    public function fxorderlist()
    {
        $uid = $this->auth->id;
        $main_id=$this->request->param('main_id');
        $channel_type = $this->request->param('channel_type');
        
        
        $where = ['me.user_id'=>$uid];
        $field = 'mo.order_id,mo.commission,mo.createtime,mt.title,me.channel_type,mo.status';
        
        if ($main_id) {
            $where['mo.main_id'] = $main_id;
        }
        
        if ($channel_type) {
            $where['me.channel_type'] = $channel_type;
        }
        
        
        $stime = $this->request->param('stime');
        $etime = $this->request->param('etime');
        if ($stime&&$etime) {
            $where['mo.createtime'] = ['BETWEEN',[strtotime($stime.'00:00:00'),strtotime($etime.' 23:59:59')]];
        }
        
        
        
        $getChanneltypeList = $this->getChanneltypeList();
        $list = Db::name('mental_order')
            ->alias('mo')
            ->join('mental_extension me','mo.extensionId=me.id')
            ->join('mental_testmain mt','mo.main_id=mt.id')
            ->where($where)
            ->field($field)
            ->order('mo.createtime DESC')
            ->paginate(20)
            ->each(function($item, $key)use($getChanneltypeList){
                $item['str'] = $getChanneltypeList[$item['channel_type']];
                return $item;
            });
        
        $where1 = $where;
        $where1['mo.status'] = 'paid';
        //当前查询订单佣金总额
        $TotalOrder = Db::name('mental_order')->alias('mo')->join('mental_extension me','mo.extensionId=me.id')->join('mental_testmain mt','mo.main_id=mt.id')->where($where1)->sum('mo.commission');
        
        $where2 = ['me.user_id'=>$uid,'mo.status'=>'paid'];
        //累计总收益
        $ljsy = Db::name('mental_order')->alias('mo')->join('mental_extension me','mo.extensionId=me.id')->join('mental_testmain mt','mo.main_id=mt.id')->where($where2)->sum('mo.commission');
        
        //今日累计总收益
        $jrljsy = Db::name('mental_order')->alias('mo')->join('mental_extension me','mo.extensionId=me.id')->join('mental_testmain mt','mo.main_id=mt.id')->where($where2)->whereTime('mo.createtime','today')->sum('mo.commission');
        
        
        $ret = [
            'list'=>$list,//订单列表
            'TotalOrder'=>$TotalOrder,//当前查询订单佣金总额
            'ljsy'=>$ljsy,//累计总收益
            'jrljsy'=>$jrljsy,//今日累计总收益
            ];
        
        $this->success('成功',$ret);
    }
    
    
    public function getChanneltypeList()
    {
        $list = [
            1  => '微博',
            2  => '百度搜索',
            3  => '小红书',
            4  => '抖音',
            5  => '快手',
            6  => '知乎',
            7  => 'SOUL',
            8  => '360搜索',
            9  => '头条',
            10 => '百家号',
            11 => '头条号',
            12 => '谷歌搜索',
            13 => '必应搜索',
            14 => '搜狗搜索',
            15 => '起点小说',
            16 => '番茄小说',
            17 => '美柚柚+',
            18 => '皮皮虾短视频',
            19 => '微信朋友圈',
            20 => 'QQ空间',
            21 => '快看漫画',
            22 => '风车动漫',
            23 => '网易',
            24 => 'B站',
            25 => '七猫小说',
            26 => '蜻蜓FM',
            27 => '企鹅FM',
            28 => 'QQ阅读',
            29 => '微信读书',
            ];
        return $list;
    }
    
    
    /**
     * 渠道列表(addons/mental/moneylog/getChanneltype)
     */
    public function getChanneltype()
    {
        $list = $this->getChanneltypeList();
        
        $this->success('成功',$list);
    }
    
    
    
    /**
     * 量表列表(addons/mental/moneylog/getMainlist)
     */
    public function getMainlist()
    {
        $mainIds = Db::name('mental_extension')->where(['user_id'=>$this->auth->id])->column('main_id');
        
        
        $list = Db::name('mental_testmain')
            ->where(['id'=>['IN',$mainIds]])
            ->field('id,title')
            ->order('id DESC')
            ->select();
            
        $this->success('成功',$list);
    }
    
}
